import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

//处理antd Table数据，返回columns
export const  renderColumn = (obj, title, origin, fun) => {
  const columns = [];
  let i = 0;
  for( let k in obj) {
    var colObj = new Object;
    if(
      k === 'key' || k == 'requestid' || k == 'requestnametitle' 
    || k == 'stayTime' || k == 'isover' || k == 'belongToShow' 
    || k == 'f_weaver_belongto_userid' || k == 'f_weaver_belongto_usertype'
    ) continue;
    const kv = obj[k];
    if(kv && typeof kv === 'object' && kv.constructor === Object) {
      colObj = {
        dataIndex: k,
        classname:  k === 'requestname' ? 'reqdetail':'',
        key: k,
        render: (text) => {
          if(text.createrid) {
            var ahtml = "<a href='javascript:openhrm("+ text.createrid +");' onclick='pointerXY(event)' title="+text.creatername+"><font class='font wea-f"+"12px"+"'>"+text.creatername+"</font></a>";
            return <span dangerouslySetInnerHTML={{__html:ahtml}}></span>
          }
          if(text.userid) {
            var ahtml = "<a href='javascript:openhrm("+ text.userid +");' onclick='pointerXY(event)' title="+text.name+"><font class='font wea-f"+"12px"+"'>"+text.name+"</font></a>";
            return <span dangerouslySetInnerHTML={{__html:ahtml}}></span>
          }
        },
      }
    } else if(kv &&  k == 'requestname') {
      colObj = {
        dataIndex: k,
        classname:  'requestname',
        key: k,
        render: (str, obj) => loadRemind(str, obj, origin, fun),
      }
    } else { 
      colObj = {
        dataIndex: k,
        key: k,
        render: (text) => {
          if(typeof text === 'string' && k == 'subwf'){
            return <div title='详细' dangerouslySetInnerHTML={{__html:text}}></div>
          } else {
            return <div dangerouslySetInnerHTML={{__html:text}}></div>
          }
        }
      }
    }
  // 表格顶部列名显示
  if (title) colObj['title'] = title[i];

    let itemWidth = (100/(title.length+2)).toFixed(2) + '%';
    if(k == 'requestname') {
      colObj.width = (100/(title.length+2)).toFixed(2) * 3 + '%';
    } else {
      colObj.width = itemWidth;
    }
    
    columns.push(colObj);
   i += 1;
  }
  return columns
}
const loadRemind = (htmlStr, obj, origin, fun) => {
  let str = htmlStr;
  let dom1 = null;
  let stayStr = '';
  if(origin == 'backlog') {
    if(!(obj.requestname.indexOf('<a') < 2)) { //开启在主流程显示功能
      dom1 = jQuery(str);
    } else {
      dom1 = jQuery(`<div>${str}</div>`);
    }
    let userid = -1, usertype = -1;
    if(obj.f_weaver_belongto_userid) {
      userid = obj.f_weaver_belongto_userid
    } 
    if(obj.f_weaver_belongto_usertype) {
      usertype = obj.f_weaver_belongto_usertype;
    }
    let imgStr = "<img class='workflow_center_signaturesIcon' title='"+ getLabel(519126,'查看流转意见') +"' src='/images/ecology8/homepage/req_wev8.png' style='width: 18px; height: 18px; display:none; cursor: pointer;' onclick={openCenter(" + obj.requestid + ',' + userid + ',' + usertype +  ")} />"
    let dom2 = jQuery(imgStr);
    let str2 = dom1.append(dom2).html();
    if(obj.isover) {
      stayStr = "<p style='color: #999'>" +  getLabel('519127', '已停留')  +"<span style='color: #e74c3c; margin-left: 5px'>" + obj.stayTime + "</span></p>"; 
    } else {
      stayStr = "<p style='color: #999'>" + getLabel('519127', '已停留')   +"<span style='margin-left: 5px'>" + obj.stayTime + "</span></p>"; 
    }
    str = "<div class='wf-title-wrap' style='width:98%'  title='" + obj.requestnametitle + "'>" +str2 +"</div>" + stayStr;
  }
  let content = (
    <div>
      <div dangerouslySetInnerHTML={{__html:str}}/>
    </div>
  )
 return (content)
 
 
}


export const imgUtil = (srcHtml__)  => {
  if (srcHtml__.indexOf("small_pic") > -1 || srcHtml__.indexOf("SMALL_PIC") > -1) {
      var firstnum = srcHtml__.indexOf("<div");
      if (firstnum == -1) {
          firstnum = srcHtml__.indexOf("<DIV");
      }
      var begin_srcHtml = srcHtml__.substring(0, firstnum);
      var cycle_begin = "";
      var new_srcHtml = "";
      var end_srcHtml = "";
      var divwords = "";
      //alert(srcHtml__);
      var cycleString = srcHtml__.substring(firstnum);
      while (cycleString.indexOf("<img") > -1 || cycleString.indexOf("<IMG") > -1) {
          
          //处理pc端发表签字意见带图片的情况，获取文本内容
          var i = cycleString.indexOf("<p") || cycleString.indexOf('<P');
          if(i !== -1)  {
            var textTemp = cycleString.substring(i);
            var z = textTemp.indexOf("<div");
            var text = textTemp.substring(0, z);
            begin_srcHtml += text;
          }
        
          //得到div图片之间的文字
          var j = cycleString.indexOf("</div");
          if (j == -1) {
              j = cycleString.indexOf("</DIV");
          }
          var divwordstemp = cycleString.substring(j + 6);
          var k = divwordstemp.indexOf("<div");
          if (k == -1) {
              k = divwordstemp.indexOf("<DIV");
          }
          divwords = divwordstemp.substring(0, k);
          begin_srcHtml += divwords;
          //第一个img
          var b = cycleString.indexOf("<img");
          if (b == -1) {
              b = cycleString.indexOf("<IMG");
          }

          cycleString = cycleString.substring(b);
          var e = cycleString.indexOf(">");
          cycleString = cycleString.substring(e + 1);
          //第二个img
          var m = cycleString.indexOf("<img");
          if (m == -1) {
              m = cycleString.indexOf("<IMG");
          }
          cycleString = cycleString.substring(m);
          var n = cycleString.indexOf(">");
          let imgString = cycleString.substring(0, n)+"/>";
          cycleString = cycleString.substring(n + 1);
          //懒加载图片需要处理
          let imgSwapObj = jQuery(`<div>${imgString}</div>`);
          let imgObj = imgSwapObj.find("img");
          if(imgObj.attr("original-src"))
              imgObj.removeAttr("src").attr("src", imgObj.attr("original-src")).removeAttr("original-src");
          if(imgObj.attr("original-style"))
      imgObj.removeAttr("style").attr("style", imgObj.attr("original-style")).removeAttr("original-style");
          imgString = imgSwapObj.html().replace(">", "/>");
          imgString = imgString.replace("class=\"maxImg\"", "");
          begin_srcHtml += "<div> " + imgString + "</div>";
          end_srcHtml = cycleString;
      }
      var r = end_srcHtml.indexOf("</div>");
      if (r == -1) {
          r = end_srcHtml.indexOf("</DIV>");
      }
      end_srcHtml = end_srcHtml.substring(r + 6);
      begin_srcHtml += end_srcHtml;

      srcHtml__ = begin_srcHtml;
  }
  return srcHtml__;

}

//待办已办数据改造
export const reformData = (data={}) => {
  let columnsTitle = [],
      dataSource = [],
      tabs = [];
  if(!data.showCol || !data.datas) return {
      columnsTitle: columnsTitle,
      dataSource: dataSource,
      tabs: tabs
    }
  let obj = {}
  data.showCol && data.showCol.length > 0 && data.showCol.map(v => {
    columnsTitle.push(v.fieldshowname)
    obj[v.fielddbname] = v.fielddbname
  })
  data.datas && data.datas.length > 0 && data.datas.map ((v,index) => {
    let temp = {};
    temp.key = index;
    for(let i in obj) {
      if(i != 'requestname' && i != 'creater' && i != 'subwf' && i != 'unoperators' && i) {
        temp[i] = '<div class="wrap-div" title="'+ v[i] +'">' + v[i]+ '</div>';
      } else {
        temp[i] = v[i];
      }
    }
    temp.requestid = v.requestid;
    temp.requestnametitle = v.requestnametitle;
    temp.stayTime = v.stayTime;
    temp.isover = v.isover;
    temp.belongToShow = v.belongToShow;
    temp.f_weaver_belongto_userid = v.f_weaver_belongto_userid;
    temp.f_weaver_belongto_usertype = v.f_weaver_belongto_usertype;
    dataSource.push(temp);
  })
  data.tabs && data.tabs.length > 0 && data.tabs.map (v => {
    let obj = {};
    if(v.id) {
      obj.key = v.id;
    } else if(v.typeid) {
      obj.key = v.typeid;
    }
    obj.title = v.showname;
    tabs.push({...obj, ...v});
  })
  return  {
    columnsTitle: columnsTitle,
    dataSource: dataSource,
    tabs: tabs
  }
}
